package com.example.Socket;

import java.net.*;
import java.io.*;

// 客户端
public class GreetingClient {
    public static void main(String[] args) {
//        String serverName = args[0];  // 连接到主机
        String serverName = "localhost";
        int port = 6066;
//        int port = Integer.parseInt(args[1]); // 端口号

        try {
            System.out.println("连接到主机：" + serverName + " ，端口号：" + port);
            Socket client = new Socket(serverName, port);
//            ServerSocket socket = new ServerSocket(port);
            System.out.println("远程主机地址：" + client.getRemoteSocketAddress());
            // 返回此套接字的输出流。
            OutputStream outToServer = client.getOutputStream();
            DataOutputStream out = new DataOutputStream(outToServer);

            out.writeUTF("主机来自："+client.getLocalSocketAddress());

            //返回此套接字的输入流。
            InputStream inFromServer = client.getInputStream();
            DataInputStream in = new DataInputStream(inFromServer);
            System.out.println("服务器响应： " + in.readUTF());
            client.close();
        } catch (IOException e){
            e.printStackTrace();
        }

    }
}
